<template>
  <div class="flow-box">
    <el-table
        :data="tableData"
        stripe
        style="width: 100%;height: 91vh;"
        border
        max-height="91vh"
        empty-text="暂无数据"
    >
      <el-table-column
          fixed
          prop="seal_flow_id"
          label="ID"
          width="200"
          align="center"
      />
      <el-table-column
          label="申请人"
          width="100"
          align="center"
      >
        {{userInfo.username}}
      </el-table-column>
      <el-table-column
          label="申请时间"
          width="120"
          align="center"
      >
        <template #default="{row}">
          {{computeDate(row.created_at)}}
        </template>
      </el-table-column>
      <el-table-column
          label="状态"
          align="center"
          width="120"
      >
        <template #default="{row}">
          <el-tag type="info" v-if="row.seal_flow_status === 0">已提交</el-tag>
          <el-tag type="warning" v-else-if="row.seal_flow_status === 1">审批中</el-tag>
          <el-tag type="warning" v-else-if="row.seal_flow_status === 2">已完成</el-tag>
          <el-tag type="info" v-else-if="row.seal_flow_status === 3">已撤销</el-tag>
          <el-tag type="warning" v-else-if="row.seal_flow_status === 4">驳回待修改</el-tag>
          <el-tag type="info" v-else-if="row.seal_flow_status === 5">修改待审批</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="status"
          label="当前结果"
          align="center"
          width="120"
      >
        <template #default="{row}">
          <el-tag type="warning" v-if="row.seal_flow_result === 0">拒绝</el-tag>
          <el-tag type="success" v-else-if="row.seal_flow_result === 1">通过</el-tag>
          <el-tag type="info" v-else>进行中</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="operatorName"
          label="当前/最后审批人"
          align="center"
          width="150"
      />
      <el-table-column
          label="是否外借"
          align="center"
          width="100"
      >
        <template #default="{row}">
          <el-tag type="warning" v-if="row.seal_take === 0">否</el-tag>
          <el-tag type="success" v-else-if="row.seal_take === 1">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          label="是否归还"
          align="center"
          width="100"
      >
        <template #default="{row}">
          <el-tag type="warning" v-if="row.seal_flow_return === 0">未归还</el-tag>
          <el-tag type="success" v-else-if="row.seal_flow_return === 1">已归还</el-tag>
          <el-tag type="success" v-else>未借出</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="seal_flow_use_number"
          label="盖章次数"
          align="center"
          width="80"
      />
      <el-table-column
          label="理由/说明/备注"
          width="300"
          align="center"
      >
        <template #default="{row}">
          {{ row.details ? row.details : '-'}}
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
          align="center"
      >
        <template #default="{row}">
          <el-button
              type="primary"
              size="small"
              @click="showDetails(row.seal_flow_id)"
          >
            详情
          </el-button>
          <el-button
              type="primary"
              size="small"
              @click="showEdit(row.seal_flow_id)"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <PreViewDialog ref="previewDialogRef" @refreshAll="getEditList"/>

  <EditFormDialog ref="editDialogRef"  @refreshEdit="getEditList"/>
</template>

<script setup>
import {computeDate, createMsg, getUserInfo} from "@/lib/utils"
import {ElButton, ElForm, ElTable, ElTableColumn, ElTag, ElUpload} from "element-plus"
import {ref} from "vue-demi"
import {createSealApprove, getEdit, getSealApproveDetails, submitEdit} from "@/api/OA/Seal"
import {onMounted, reactive} from "vue"
import PreViewDialog from "@/components/Common/OA/Seal/PreviewSealDialog.vue"
import emitter from "@/lib/eventBus"
import {resubmitURL} from "@/api/http"
import {checkUserByRole} from "@/api"
import EditFormDialog from "@/components/Common/OA/Seal/EditForm.vue"

let userInfo
const previewDialogRef = ref()
const editDialogRef = ref()
const editDialogVisible = ref(false)
const seal_file = ref([])
const tableData = ref([])

//审核人级联选择
const copyOptions = ref([])
// 章类型 0 公章 1 法人章 2 合同章
const sealOption = [
  {
    value: 0,
    label: '公章',
  },{
    value: 1,
    label: '法人章',
  },{
    value: 2,
    label: '合同章',
  },
]

// 是否外接 0否 1是
const takeOption = [
  {
    value: 0,
    label: '否',
  },{
    value: 1,
    label: '是',
  },
]
let seal_form = reactive({})

onMounted(() => {
  userInfo = getUserInfo()
  getEditList()
})

const getEditList = async () => {
  try {
    const {data:res} = await getEdit(userInfo.id)
    if (res.code === 200) {
      tableData.value = res.data
    } else {
      tableData.value = []
    }
  } catch (e) {
    console.log(e)
  }
}

const showDetails = (id) => {
  previewDialogRef.value.showDetails(id)
}

const showEdit = async (id) => {
  try {
    console.log(id)
    editDialogRef.value.showDetails(id)
  } catch (e) {
    console.log(e)
  }
}



const handleExceed = () => {
  createMsg('单次只能上传一个文件','warning')
}

const handleSealFileChange = (file) => {
  console.log(file.raw,'file.raw')
  seal_file.value.push(file.raw)
}

const handleSealFileSuccess = (file) => {
  console.log(file,'s')
}

const handleClose = () => {
  editDialogVisible.value = false
  seal_form = {}
}

const onSubmit = async () => {
  const userConfirm = window.confirm('此操作仅对已填写的内容进行修改，是否提交？')
  if (userConfirm){
    const form = new FormData();
    form.append("userId", user_info.id);
    form.append("sealType", seal_form.sealType);
    form.append("sealSubject", seal_form.sealSubject);
    form.append("sealProject", seal_form.sealProject);
    form.append("sealTake", seal_form.sealTake);
    form.append("sealUseNumber", seal_form.sealUseNumber-0);
    form.append("file",seal_file.value[0])
    console.log(seal_file.value[0])
    const {data:res} = await submitEdit(form)
    if (res.code === 200){
      await getEditList()
      handleClose()
      return createMsg('操作成功!','success',true)
    } else {
      handleClose()
      console.log(res)
      return createMsg('操作失败','info',true)
    }
  }
}

</script>

<style scoped lang="less">

</style>
